import { Component, OnInit } from '@angular/core';
import {TipService} from "../../services/tip.service";
import {ExtendHttpService} from "../../../common/extend-http.service";
import {Router} from "@angular/router";

@Component({
  selector: 'app-today-coutn',
  templateUrl: './today-coutn.component.html',
  styleUrls: ['./today-coutn.component.css']
})
export class TodayCoutnComponent implements OnInit {

  tipType = 'error';
  isLoading = false;
  list = [];
  isFlag = false;
  loading = false;
  params = {time: ''};
  result = {card: {},totalObj: {},payList: [],printList: [],doshesList:[]};
  constructor(
    private tipService: TipService,
    private extendHttp: ExtendHttpService,
    private router: Router
  ) { }
  ngOnInit() {
    cBack = () => {
      $('#btn-back').trigger('click');
    }
    this.getData();
    var nowEnd = new Date();

    var nowStart = new Date();
    nowStart.setMonth(nowStart.getMonth() );
    var monthStart = nowStart.getMonth() + 1;
    var dayStart = nowStart.getDate();

    var m1 = [];
    var d1 = [];
    if (monthStart < 10){
      m1.push(0);
      m1.push(monthStart);
    }
    if (dayStart < 10) {
      d1.push(0);
      d1.push(dayStart);
    } else {
      d1.push(dayStart)
    }
    var todayStart = nowEnd.getFullYear() + '-' + m1.join('') + '-' + d1.join('');
    this.params.time = todayStart;
  }

  getData(searchFlag?: boolean) {
    if (searchFlag) {
      if (!this.params.time.trim()) {
        this.tipService.change(true, '请选择时间');
        return;
      }
    }

    this.extendHttp.post('/merchantApp/Statistics/endOfDay', this.params).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {

        // 会员卡相关统计
        this.result.card = data.card;

        // 今日统计的数据
        this.result.totalObj = data.totalObj;

        // 支付方式统计
        this.result.payList = data.payList;

        // 打印机统计
        this.result.printList = data.printList;

        // 菜品分类
        this.result.doshesList = data.doshesList
        this.isFlag = true;
        this.loading = false;
      } else if (data.code == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.loading = false;
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.loading = false;
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

  clearData() {
    this.params.time = '';
  }

}
